#!/usr/bin/env python
#
# Copyright 2009 Acumino - http://www.acumino.com/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import cgi, logging, sys
sys.path.insert(0, '../')
from config import config

from google.appengine.api import users
from google.appengine.ext.webapp import template

def menu_items(path=''):
  items = config.content['menu']
  menu = "<ul>\n"
  for item in items:
    if (path == item['url']):
      menu += "  <li><a class=\"activelink\" href=\"#\">%s</a></li>\n" % item['name']
    else:
      menu += "  <li><a href=\"%s\">%s</a></li>\n" % (item['url'], item['name'])
  if (users.is_current_user_admin() and not (path[-5:] == '/edit')):
    menu += "  <li><a href=\"%s/edit\">Edit</a></li>\n" % path
  if users.get_current_user():
    menu += "  <li><a href=\"%s\">Logout</a></li>\n" % \
      cgi.escape(users.create_logout_url(path))
  else:
    menu += "  <li><a href=\"%s\">Login</a></li>\n" % \
      cgi.escape(users.create_login_url(path))
  menu += "</ul>"
  return menu  

register = template.create_template_register();
register.simple_tag(menu_items)
